package com.example.accessibilityservice.server

import android.app.Service
import android.content.Context
import android.content.Intent
import android.graphics.PixelFormat
import android.graphics.Rect
import android.os.IBinder
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.example.accessibilityservice.DrawView
import com.example.accessibilityservice.R


class FloatingService : Service() {
    lateinit var mFloatView: View
    lateinit var windowManager: WindowManager

    lateinit var mLayoutParams: WindowManager.LayoutParams

    lateinit var mLayoutInflater: LayoutInflater
    lateinit var drawView: DrawView

    companion object {
        val TAG = "FloatingService"
    }

    override fun onBind(p0: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        windowManager =
            (applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager)
        mLayoutInflater = LayoutInflater.from(this)
        createView()
    }

    fun createView() {
        // TODO Auto-generated method stub
        //加载布局文件
        // TODO Auto-generated method stub
        //加载布局文件
        mFloatView = mLayoutInflater.inflate(R.layout.service_layout, null)
        drawView = mFloatView.findViewById<DrawView>(R.id.drawView)
        //为View设置监听，以便处理用户的点击和拖动
        //为View设置监听，以便处理用户的点击和拖动
//        mFloatView.setOnTouchListener(OnFloatViewTouchListener())
        /*为View设置参数*/
        /*为View设置参数*/
        mLayoutParams = WindowManager.LayoutParams()
        //设置View默认的摆放位置
        //设置View默认的摆放位置
        mLayoutParams.gravity = Gravity.START or Gravity.TOP
        //设置window type
        //设置window type
        mLayoutParams.type =
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        //设置背景为透明
        //设置背景为透明
        mLayoutParams.format = PixelFormat.RGBA_8888
        //注意该属性的设置很重要，FLAG_NOT_FOCUSABLE使浮动窗口不获取焦点,若不设置该属性，屏幕的其它位置点击无效，应为它们无法获取焦点
        //注意该属性的设置很重要，FLAG_NOT_FOCUSABLE使浮动窗口不获取焦点,若不设置该属性，屏幕的其它位置点击无效，应为它们无法获取焦点
        mLayoutParams.flags = (WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
        //设置视图的显示位置，通过WindowManager更新视图的位置其实就是改变(x,y)的值
        //设置视图的显示位置，通过WindowManager更新视图的位置其实就是改变(x,y)的值
//        mCurrentX = 50.also { mLayoutParams.x = it }
//        mCurrentY = 50.also { mLayoutParams.y = it }
        //设置视图的宽、高
        //设置视图的宽、高
        mLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
        mLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
        //将视图添加到Window中
        //将视图添加到Window中
        windowManager.addView(mFloatView, mLayoutParams)
    }

    fun setRectList(list: List<Rect>) {
        println("List大小：${list.size}")
        drawView.setRectList(list)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.d(TAG, "onStartCommand: 收到数据")
        val parcelableArrayListExtra = intent?.getParcelableArrayListExtra<Rect>("rectList")
        if (parcelableArrayListExtra != null) {
            setRectList(parcelableArrayListExtra)
        }
        return super.onStartCommand(intent, flags, startId)
    }

}